//
//  AYSFileHandle.m
//  AiYingShiProject
//
//  Created by lanou on 15/11/4.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSFileHandle.h"

@implementation AYSFileHandle

+ (instancetype)shareIntance
{
    static AYSFileHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[AYSFileHandle alloc] init];
    });
    return handle;
}

#pragma mark ------数据库缓存----------

// cache路径
- (NSString *)cachesPath
{
    return  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

// 数据库存储的路径
- (NSString *)databaseFilepath:(NSString *)databaseName
{
    return [[self cachesPath] stringByAppendingPathComponent:databaseName];
}

// 删除数据库
- (void)removeDatabase
{
    NSString *filePath = [self databaseFilepath:@"AiYingShi1.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:filePath error:nil];
}

// 将对象归档
- (NSData *)dataOfArchiverObject:(id)object forKey:(NSString *)key
{
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:object forKey:key];
    [archiver finishEncoding];
    return data;
}

// 反归档
- (id)unarchiverObject:(NSData *)data forKey:(NSString *)key
{
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    id object = [unarchiver decodeObjectForKey:key];
    [unarchiver finishDecoding];
    return object;
}













@end
